feat: add SQLCommenter observability to database drivers (fixes #2899)#2913
feat: add SQLCommenter observability to database drivers (fixes #2899)#2913tanujbolisetty wants to merge 7 commits into
Conversation
|
Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA). View this failed invocation of the CLA check for more information. For the most up to date status, view the checks section at the bottom of the pull request. |
There was a problem hiding this comment.
Code Review
This pull request implements the SQLCommenter observability layer across all SQL-capable database drivers, enabling query tracking by tool name, agent identity, and trace metadata. It introduces the internal/sqlcommenter package and updates the MCP server to persist agent names across sessions. Feedback identifies a memory leak in the session name map, suggests refactoring duplicated RunSQL logic in the CockroachDB source, and recommends using %20 for space encoding to ensure strict adherence to the SQLCommenter specification.
9836bd1 to
b24f64f
Compare
…bstraction drivers
3760269 to
09dcc7f
Compare
|
Can you please integrate the latest changes on main so we can see what is actually changing? |
…ramework defaults
90dbf31 to
4102ec5
Compare
|
"I've just successfully rebased onto the latest upstream/main (v1.0.0 mcp-toolbox namespace) and squashed the history. The PR is now completely up-to-date with a clean commit trail!" |
There was a problem hiding this comment.
Hi @tanujbolisetty Thank you for your contributions! Please let me know if you need any clarifications. :)
There was a problem hiding this comment.
These changes should be reversed. We did not update the llamaindex sdk repo name~
| - "Execute a DML query to update customer names." | ||
| - "List all tables in the `my-database`." | ||
| - "Execute a DQL query to select data from `orders` table." |
There was a problem hiding this comment.
Let's keep to * to be consistent with other readmes.
There was a problem hiding this comment.
Changes in this file should be reversed. It is redundant for this sqlcommenter.
There was a problem hiding this comment.
This doc should not be included here. We can add sqlcommenter under docs/en/documentation/monitoring. Please modify this file to fit with the docs template format. Alternatively, since every source's sqlcommenter is different, we can also add it under each docs/en/integrations source's docs.
| | **CloudSQL Postgres** | `internal/sources/cloudsqlpg/cloud_sql_pg.go` | SQL Comment (`/* ... */`) | | ||
| | **CockroachDB** | `internal/sources/cockroachdb/cockroachdb.go` | SQL Comment (`/* ... */`) | | ||
| | **Couchbase (N1QL)** | `internal/sources/couchbase/couchbase.go`<br>`internal/tools/couchbase/couchbase.go` | N1QL Comment (`/* ... */`) | | ||
|
|
There was a problem hiding this comment.
let's remove this file. All docs should go under docs. We can concatenate the information here along with the other sqlcommenter documentation.
| // Embed the tool name in the context so RunSQL can attach it as a BigQuery | ||
| // Job Label for Cloud SQL Insights / BigQuery observability. | ||
| ctx = sqlcommenter.WithToolName(ctx, t.Name) | ||
|
|
There was a problem hiding this comment.
This should be added in server/mcp?
There was a problem hiding this comment.
Please reverse, this change is redundant~
| func (s *Source) RunSQL(ctx context.Context, statement string, params parameters.ParamValues, isQuery bool, timeout string) (any, error) { | ||
| // Dgraph does not support SQL block comments, so we explicitly skip injecting SQLCommenter metadata here. | ||
|
|
There was a problem hiding this comment.
since we are not using ctx here, please reverse this.
There was a problem hiding this comment.
redundant, please reverse~
🚀 Overview
This PR introduces SQLCommenter observability across all supported database drivers in the GenAI Toolbox. It automatically injects rich contextual metadata (tool name, framework, agent controller, database driver, and OpenTelemetry traceparent) into every query executed by the toolbox.
By standardizing this telemetry pattern, downstream database administrators and cloud monitoring tools (like Cloud SQL Insights) can seamlessly map database loads back to specific AI agents and MCP tools without any manual parsing.
🛠 Features & Modifications
1. Comprehensive Driver Coverage
We have instrumented the
RunSQLmethod on 100% of SQL-capable database sources.The metadata is injected safely as a block comment (
/* ... */) before the trailing semicolon (where applicable) to preserve syntax parsing.alloydbpg,clickhouse,cockroachdb,cloudsqlmssql,cloudsqlmysql,cloudsqlpg,firebird,mindsdb,mssql,mysql,oceanbase,oracle,postgres,singlestore,snowflake,spanner,sqlite,tidb,trino,yugabytedb.cassandra(CQL) andcouchbase(N1QL). For Couchbase, the SourceRunSQLsignature and corresponding Tool wrapperInvokeinterface were refactored to explicitly acceptcontext.Contextto enable propagation.bigqueryis instrumented via native BigQuery Job Labels instead of SQL Comments, leveraging the same contextual map.mongodb,firestore,redis,bigtable,dgraph) were explicitly skipped as they do not support standard SQL comment blocks, preventing catastrophic parsing crashes.2. Context Injection Pipeline
Created
internal/sqlcommenter/commenter.goto handle context reading, deterministic key sorting, formatting (RFC 3986 URL encoding), and OpenTelemetry span extraction.MCP Auto-Context Pipeline:
internal/server/server.gowas updated to trap theclientInfo.namepayload from the MCPinitializehandshake in a concurrentmcpClientNamessync map.api.goextracts this dynamically during tool invocation to populate thecontrollerfield automatically (e.g. associating queries directly with "Mosa" or "Claude Desktop").Fallback configurations
TOOLBOX_APP_NAMEandTOOLBOX_AGENT_NAMEwere established to manually override the context if needed.3. Core Framework & Build Architecture Upgrades
These changes resolve significant localized testing and binary compilation blockers encountered during development:
internal/server/server.goconfiguration booting logic so that passing the--uiconfig implicitly turns on the REST API endpoints (if cfg.EnableAPI || cfg.UI).oci_cgo.goandoci_pure.go). By leveraging Go build tags (//go:build cgo), the toolbox now dynamically compiles using the lightweightgo-ora/v2Pure Go driver by default, entirely sidestepping notoriously complexgodrorC compiler dependencies native to Oracle cross-compilation.🧪 Verification
go test ./internal/sqlcommenter/....WithDBDriverandWithToolNamecontext decorators directly, avoiding the need for live mocked database instances in CI.go build ./...successfully compiles across pure GO targets natively.📝 Documentation
Overhauled
docs/SQLCOMMENTER_README.mdand explicitly mapped out the full driver support matrix, environment overrides, and NoSQL exclusions.Recommendation: Ensure
DEVELOPER.mdor the primaryREADME.mdis updated to highlight that the default codebase cross-compiles with Oracle's PureGo driver to unblock new developers!